package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.result.ResultUtil;
import com.ctcemti.mdm.common.util.ExcelParseUtils;
import com.ctcemti.mdm.project.sys.entity.InfDictItem;
import com.ctcemti.mdm.project.sys.entity.OrgJob;
import com.ctcemti.mdm.project.sys.service.IJobService;
import com.ctcemti.mdm.project.sys.service.InfDictItemService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "JobController", description = "岗位职责控制器类")
@RequestMapping(value="job")
public class JobController {

    @Autowired
    IJobService jobService;
    @Autowired
    InfDictItemService infDictItemService;

    @PostMapping(value = "insertOrUpdate")
    public Result insertOrUpdate(OrgJob orgJob){
        jobService.insertOrUpdate(orgJob);
        return ResultUtil.success();
    }

    @PostMapping(value="importJobs")
    public Result importJobs(@RequestParam("file") MultipartFile file){
        try{
            List<OrgJob> list = ExcelParseUtils.importExcel(file,0,1,OrgJob.class);
            List<InfDictItem> infDictItemList = infDictItemService.selectInfdictitemByInfdictsortCode("");
            Map<String,String> tempmap = new HashMap<String, String>();
            if(infDictItemList.size()>0){
                for (int i=0;i<infDictItemList.size();i++){
                    tempmap.put(infDictItemList.get(i).getName(),infDictItemList.get(i).getCode());
                }
                for(OrgJob o:list){
                    //查询job的值
                    String job = tempmap.get(o.getJob())==null?"1":String.valueOf(tempmap.get(o.getJob()));
                    o.setJob(job);
                    jobService.insertOrUpdate(o);
                }
            }else{
                for(OrgJob o:list){
                    //查询job的值
                    String job = "1";
                    o.setJob(job);
                    jobService.insertOrUpdate(o);
                }
            }


        }catch(Exception e){
            return ResultUtil.error(ResultEnum.ZTSJ_1021);
        }
        return ResultUtil.success("导入成功");
    }

    @PostMapping("delete")
    public Result delete(String idStr){
        jobService.delete(idStr);
        return ResultUtil.success();
    }

    @PostMapping("getJobs")
    public Result getJobs(Integer page,Integer limit,String job){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("page",page);
        param.put("limit",limit);
        param.put("job",job);
        PageInfo<OrgJob> jobs = jobService.getOrgJob(param);
        return ResultUtil.success(jobs);
    }

    @PostMapping("getJobByCode")
    public Result getJobByCode(HttpServletRequest request,String code){
        List<OrgJob> job = jobService.getJobByCode(code);
        return new Result(ResultEnum.SUCCESS,job);
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("uploadFile")
    public Result<Map<String,Object>> uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CommonsMultipartResolver commonsMultipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartFile multipartFile = null;
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
            Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
            while (iterator.hasNext()){
                multipartFile= multipartHttpServletRequest.getFile(iterator.next());
            }
        }
        Map<String,Object> result = jobService.uploadFile(multipartFile,response,request);
        return new Result<Map<String,Object>>(ResultEnum.SUCCESS,result);
    }

}
